<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>药品信息管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo layui-hide-xs layui-bg-black">小药店管理系统</div>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                <a>
                    <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                    欢迎, <span id="username">管理员</span>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="javascript:;" onclick="logout()">退出</a></dd>
                </dl>
            </li>
        </ul>
    </div>

    <!-- 左侧导航区域 -->
    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航内容 -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">采购管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="supplier.html">供应商信息管理</a></dd>
                        <dd><a href="drug.html">药品信息管理</a></dd>
                        <dd><a href="../procurement/add.html">采购单添加</a></dd>
                        <dd><a href="../procurement/list.html">采购单管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">销售管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../sales/add_xiaoshoudan.html">销售单添加</a></dd>
                        <dd><a href="../sales/xiaoshoumingxi.html">销售单管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">药店统计管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../statistics/instock.html">入库统计</a></dd>
                        <dd><a href="../statistics/sales.html">销售统计</a></dd>
                        <dd><a href="../statistics/stock.html">库存查询</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">人事管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../yuangong/add_yuangong.html">员工添加</a></dd>
                        <dd><a href="../yuangong/yuangong.html">员工管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">打印管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../print/ruku.html">入库单</a></dd>
                        <dd><a href="../print/chuku.html">出库单</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">系统管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../user/xiuGaiMiMa.html">修改密码</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>

    <!-- 内容主体区域 -->
    <div class="layui-body">
        <div style="padding: 15px;">
            <!-- 工具栏 -->
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-normal" onclick="openAddForm()">
                        <i class="layui-icon">&#xe608;</i> 添加药品
                    </button>
                </div>
                
                <!-- 数据表格 -->
                <div class="layui-card-body">
                    <table id="drugTable" lay-filter="drugTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 添加表单弹窗 -->
<div id="formLayer" style="display: none; padding: 20px;">
    <form class="layui-form" id="drugForm" lay-filter="drugForm">
        <div class="layui-form-item">
            <label class="layui-form-label">药品类别</label>
            <div class="layui-input-block">
                <select name="categoryId" lay-verify="required" onclick="loadCategories()">
                    <option value="">请选择类别</option>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">药品名称</label>
            <div class="layui-input-block">
                <input type="text" name="drugName" required lay-verify="required" 
                       placeholder="请输入药品名称" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-block">
                <select name="supplierId" lay-verify="required" onclick="loadSuppliers()">
                    <option value="">请选择供应商</option>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-block">
                <input type="text" name="unit" required lay-verify="required" 
                       placeholder="请输入单位" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
                <input type="number" name="price" required lay-verify="required" 
                       placeholder="请输入单价" class="layui-input" step="0.01" min="0">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">保质期</label>
            <div class="layui-input-block">
                <input type="text" name="expiryDate" required lay-verify="required" 
                       placeholder="请选择保质期" class="layui-input" id="expiryDate">
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="drugForm">确认提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
</body>

<!-- 表格操作列模板 -->
<script type="text/html" id="operationBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    // 退出功能
    function logout() {
        layui.use(['jquery', 'layer'], function(){
            var $ = layui.$;
            var layer = layui.layer;
            
            $.ajax({
                url: '/user/logout',
                type: 'GET',
                success: function(res) {
                    if(res.code === 0) {
                        window.location.href = '/login/login.html';
                    } else {
                        layer.msg(res.msg || '退出失败', {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('服务器错误', {icon: 2});
                }
            });
        });
    }

    // 初始化表格和表单
    layui.use(['table', 'form', 'laydate'], function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        $ = layui.jquery

        // 初始化表格
        table.render({
            elem: '#drugTable',
            url: '../api/drug/list',
            page: true,
            cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'categoryName', title: '药品类别', width: 120},
                {field: 'drugName', title: '药品名称', width: 150},
                {field: 'supplierName', title: '供应商', width: 150},
                {field: 'unit', title: '单位', width: 100},
                {field: 'expiryDate', title: '保质期', width: 120},
                {field: 'remark', title: '备注'},
                {title: '操作', toolbar: '#operationBar', width: 150, fixed: 'right'}
            ]],
            response: {
                statusCode: 0
            },
            parseData: function(res){
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data ? res.data.length : 0,
                    "data": res.data || []
                };
            }
        });

        // 监听工具条事件
        table.on('tool(drugTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认删除此记录？', function(index){
                    $.ajax({
                        url: '../api/drug/delete/' + data.id,
                        type: 'DELETE',
                        success: function(res){
                            if(res.code === 0){
                                layer.msg('删除成功');
                                obj.del();
                            }else{
                                layer.msg(res.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        // 初始化日期选择器
        laydate.render({
            elem: '#expiryDate'
        });

        // 表单提交
        form.on('submit(drugForm)', function(data){
            var formData = data.field;
            // 添加必要的字段
            formData.status = 1;
            formData.price = parseFloat(formData.price);  // 确保价格是数字类型
            formData.dizhi = '';
            formData.lianxiren = '';
            formData.dianhua = '';
            formData.youbian = '';
            formData.chuanzhen = '';
            formData.youxiang = '';
            
            $.ajax({
                url: '../api/drug/add',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res){
                    if(res.code === 0){
                        layer.msg('添加成功', {icon: 1});
                        layer.closeAll('page');
                        table.reload('drugTable');
                    } else {
                        layer.msg(res.msg || '添加失败', {icon: 2});
                    }
                }
            });
            return false;
        });
    });

    // 打开添加表单
    function openAddForm() {
        layer.open({
            type: 1,
            title: '添加药品',
            area: ['500px', '600px'],
            content: $('#formLayer'),
            success: function(){
                loadCategories();
                loadSuppliers();
            }
        });
    }

    // 加载药品类别
    function loadCategories(){
        $.get('../api/drug/categories', function(res){
            if(res.code === 0){
                var html = '<option value="">请选择类别</option>';
                res.data.forEach(function(item){
                    html += '<option value="' + item.id + '">' + item.categoryName + '</option>';
                });
                $('select[name="categoryId"]').html(html);
                layui.form.render('select');
            }
        });
    }

    // 加载供应商
    function loadSuppliers(){
        $.get('/supplierInformation/selectAll', function(res){
            if(res.code === 200){
                var html = '<option value="">请选择供应商</option>';
                res.data.forEach(function(item){
                    html += '<option value="' + item.id + '">' + item.mingcheng + '</option>';
                });
                $('select[name="supplierId"]').html(html);
                layui.form.render('select');
            }
        });
    }
</script>
</html> 